package com.dgw.controller;

import com.dgw.entity.User;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @program: spring-boot-web-restfulcrud
 * @description: 登录
 * @author: Mr.Dai
 * @create: 2019-11-30 20:22
 **/
@Controller
@Api(value = "登录API",tags = "登录API接口")
public class LoginController {

    @ApiResponses({
            @ApiResponse(code = 200,message = "成功！"),
            @ApiResponse(code = 401,message = "未授权！"),
            @ApiResponse(code = 404,message = "页面未找到！"),
            @ApiResponse(code = 403,message = "出错了！")
    })
    @ApiOperation(value = "登录请求方法",notes = "/user/login3 注意问题点",tags={"获取用户信息copy"})
    @GetMapping(value = "/user/login3")
    public String login2(@ApiParam(value = "用户名",name = "username",required = true) String username,
                         @ApiParam(value = "密码",name = "password",required = true) String password){
        return "index";
    }



    @ApiOperation(value = "登录请求方法",notes = "",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "username",value = "用户名",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "password",value = "密码",required = true,dataType = "String")
    })
    @PostMapping("/user/login")
    public String login(@ApiParam(name="用户对象",value="传入json格式",required=true) User user, Map<String,String> map, HttpSession session){
        if (!StringUtils.isEmpty(user.getUsername())&&"123".equals(user.getPassword())){
            // 放到session 中
            session.setAttribute("usernames",user.getUsername());
            // 要与 URL 对应处理
            return "redirect:/main.html";
        }else {
            //  填写错误msg
            map.put("msg","密码错误");
            return "login";
        }
    }


    /**
     * @RequestParam("password") 参数为前端提交过来的参数绑定
     * @param username
     * @param password
     * @return
     */
    @ApiOperation(value = "登录请求方法",notes = "",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true,dataType = "String"),
            @ApiImplicitParam(name = "password",value = "密码",required = true,dataType = "String")
    })
    @PostMapping("/user/login2")
    public String login2(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String,String> map,HttpSession session)  {
        if (!StringUtils.isEmpty(username)&&"123".equals(password)){
            // 放到session 中
            session.setAttribute("usernames",username);
            // 要与 URL 对应处理
            return "redirect:/main.html";
        }else {
            //  填写错误msg
            map.put("msg","密码错误");
            return "login";
        }
    }
}
